package com.vaadin.thomas.contenttool.demo;

import java.util.Iterator;
import java.util.Locale;

import com.github.wolfie.blackboard.Blackboard;
import com.vaadin.Application;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.service.ApplicationContext.TransactionListener;
import com.vaadin.thomas.contenttool.ContentTool;
import com.vaadin.thomas.contenttool.ui.events.LangChangedEvent;
import com.vaadin.thomas.contenttool.ui.events.LangChangedListener;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Window;

public class Contenttool_demoApplication extends Application implements
		LangChangedListener, TransactionListener {

	private static final long serialVersionUID = -4525535344770390337L;
	private ContentTool tool;
	private Blackboard bb = new Blackboard();

	private NativeSelect langSelect;
	private Label label;

	private static ThreadLocal<Contenttool_demoApplication> app = new ThreadLocal<Contenttool_demoApplication>();
	private TabSheet tabs;

	public static Contenttool_demoApplication get() {
		return app.get();
	}

	public static Blackboard blackboard() {
		return app.get().bb;
	}

	public static ContentTool contenttool() {
		return app.get().tool;
	}

	@Override
	public void init() {

		app.set(this);
		getContext().addTransactionListener(this);

		bb.register(LangChangedListener.class, LangChangedEvent.class);
		bb.addListener(this);

		Boolean preview = Boolean
				.parseBoolean(getProperty(ContentTool.PREVIEW_URIFRAGMENT) == null ? "false"
						: getProperty(ContentTool.PREVIEW_URIFRAGMENT));

		tool = new ContentTool(new SimpleConfig(), new Locale("en"));
		tool.setPreviewMode(preview);

		Window mainWindow = new Window();
		((AbstractOrderedLayout) mainWindow.getContent()).setSpacing(true);
		((AbstractOrderedLayout) mainWindow.getContent()).setSizeFull();
		setMainWindow(mainWindow);

		langSelect = new NativeSelect();
		langSelect.addItem(new Locale("en"));
		langSelect.addItem(new Locale("fi"));
		langSelect.setValue(new Locale("en"));
		langSelect.setImmediate(true);
		langSelect.setNullSelectionAllowed(false);
		langSelect.addListener(new ValueChangeListener() {

			private static final long serialVersionUID = -5949529925616828996L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				bb.fire(new LangChangedEvent());
			}
		});
		mainWindow.addComponent(langSelect);

		label = new Label();
		mainWindow.addComponent(label);

		tabs = new TabSheet();
		tabs.setSizeFull();
		tabs.addTab(new TabContent("tab1"));
		tabs.addTab(new TabContent("tab2"));

		mainWindow.addComponent(tabs);
		((AbstractOrderedLayout) mainWindow.getContent()).setExpandRatio(tabs,
				1);

		langChanged(null);
	}

	@Override
	public void langChanged(LangChangedEvent event) {

		tool.setLang((Locale) langSelect.getValue());

		getMainWindow().setCaption(tool.get("demo.windowcaption"));
		langSelect.setCaption(tool.get("demo.langselectcaption"));
		label.setValue(tool.get("demo.labelvalue"));

		Iterator<Component> iter = tabs.getComponentIterator();
		while (iter.hasNext()) {
			TabContent tc = (TabContent) iter.next();
			tabs.getTab(tc).setCaption(tc.getCaption());
		}
	}

	@Override
	public void transactionStart(Application application, Object transactionData) {
		if (application == this) {
			app.set(this);
		}
	}

	@Override
	public void transactionEnd(Application application, Object transactionData) {
		if (application == this) {
			app.set(null);
		}
	}

}
